{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "979cf9d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "edd10f17",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "120e69c1",
   "metadata": {},
   "outputs": [],
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a8ab569",
   "metadata": {},
   "source": [
    "# 2.语言识别 （百度的ASR）auto speech regonition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fce554be",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ba034629",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.202636\n"
     ]
    },
    {
     "data": {
      "text/plain": "'你好。'"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bdasr_result  = eval(bdasr.asr('1.wav'))['result'][0]\n",
    "bdasr_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f1c6abea",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"你是谁？\":\"不告诉你\",\n",
    "    '你好。':\"李俊阳最帅\",\n",
    "    '北京科技馆。':\"中国科学技术馆，位于北京市朝阳区北辰东路5号，东临亚运居住区，西濒奥运水系，南依奥运主体育场，北望森林公园，于1958年筹建，建设分为两期工程和新馆工程，分别于1988年9月22日、2000年4月29日、2009年9月16日开放。\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1a0cb280",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "'李俊阳最帅'"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qa_result = qa.get(bdasr_result)\n",
    "qa_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c718d946",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4.语言合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "00c1240f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdtts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0277d48c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUCCESS WITH TOKEN: 24.3a72f6f631d2e95c1b3853d8d2eb6034.2592000.1690292286.282335-19331335 ; EXPIRES IN SECONDS: 2592000\n",
      "test on Web Browserhttp://tsn.baidu.com/text2audio?tok=24.3a72f6f631d2e95c1b3853d8d2eb6034.2592000.1690292286.282335-19331335&tex=%25E6%259D%258E%25E4%25BF%258A%25E9%2598%25B3%25E6%259C%2580%25E5%25B8%2585&per=4&spd=5&pit=5&vol=5&aue=6&cuid=123456PYTHON&lan=zh&ctp=1\n",
      "result saved as :result.wav\n"
     ]
    }
   ],
   "source": [
    "bdtts.tts(qa_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "63dce38f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda3\\lib\\site-packages\\pydub\\utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\n",
      "  warn(\"Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work\", RuntimeWarning)\n"
     ]
    }
   ],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a0a148aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import opchat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e8947344",
   "metadata": {},
   "outputs": [],
   "source": [
    "#result = opchat.openai_chat(bdasr_result)\n",
    "#result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "27b34547",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
